//
//  GradientLabel.m
//
//  Created by mac on 2024/11/28.
//

#import "GradientLabel.h"

@implementation GradientLabel


- (void)drawTextInRect:(CGRect)rect {
    // 创建渐变层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.colors = @[
        (__bridge id)myRGBHex(0xE375FF).CGColor,
        (__bridge id)myRGBHex(0xFF0AA1).CGColor
    ];
    gradientLayer.startPoint = CGPointMake(0, 0.5);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    gradientLayer.frame = rect;
    
    // 创建渐变图像并设置为文本颜色
    UIGraphicsBeginImageContext(gradientLayer.bounds.size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *gradientImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    UIColor *gradientColor = [UIColor colorWithPatternImage:gradientImage];
    self.textColor = gradientColor;
    
    // 调用父类的绘制方法来绘制文本
    [super drawTextInRect:rect];
}

@end

